/*
 * ikakeibo 0.6.0
 * Copyright (C)2009 wateray and contributors
 * wateray@gmail.com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */
package com.wateray.ipassbook.ui;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Toolkit;

import javax.swing.JLabel;
import javax.swing.JPanel;

import org.apache.log4j.Logger;
import org.jdesktop.swingx.JXStatusBar;

import com.wateray.ipassbook.ui.perspective.CurtainCardPerspective;
import com.wateray.ipassbook.ui.view.bar.AppMenuBar;
import com.wateray.ipassbook.ui.view.bar.AppToolBar;
import com.wateray.ipassbook.ui.view.contentPanel.PageContainer;
import com.wateray.ipassbook.ui.view.contentPanel.ReceiptPage;
import com.wateray.ipassbook.ui.widget.CFrame;
import com.wateray.ipassbook.util.LanguageLoader;
import com.wateray.ipassbook.util.log4j.Log;

/**
 * @author wateray
 *
 */
public class StandardFrame extends CFrame{

	/** serialVersionUID */
	private static final long serialVersionUID = 1L;
	
	private  Logger logger = Log.getLogger(); 
	
	private static StandardFrame standardFrame;
	
	/** The application menu bar. */
	private AppMenuBar appMenuBar;
	
	/** The application tool bar. */
	private AppToolBar appToolBar;
	
	/** The application centerPane. */
	private JPanel centerPane;
	
	/** The application statusBar.*/
	private JXStatusBar statusBar;
	
	/** The left status bar. */
	private JLabel leftStatusBar;

	/** The center status bar. */
	private JLabel centerStatusBar;

	/** The right status bar. */
	private JLabel rightStatusBar;
	
	/** The main panel.*/
	private JPanel mainPanel;
	
	
	private StandardFrame(){
		super();	
		
		/** set menuBar. */
		this.setJMenuBar(getAppMenuBar());
		
		/** set ContentPane. */
		this.setContentPane(getContentPanel());
//		this.setLayout(new BorderLayout());
//		this.add(getAppToolBar(), BorderLayout.NORTH);
//		this.add(getStatusBar(), BorderLayout.SOUTH);
//		this.add(new JPanel(), BorderLayout.CENTER);
		/** application config. */
		config();
	}
	
	private Container getContentPanel(){
		/** create mainPanel. */
		mainPanel = new JPanel(new BorderLayout());
		
		/** set toolBar. */
        mainPanel.add(getAppToolBar(), BorderLayout.NORTH);
        
        /** set center panel. */
        setCenterPane(getCenterPane());
        
        /** set statusBar. */
        mainPanel.add(getStatusBar(), BorderLayout.SOUTH);
        
		return mainPanel;	
	}
	
	/**
	 * get the Center Content Pane.
	 * */
	public JPanel getCenterPane(){
		if(centerPane == null){
			centerPane = CurtainCardPerspective.getInstance();
			CurtainCardPerspective.setDefaultComponent(ReceiptPage.getInstance());
		}
		
		return centerPane;
	}
	
	/**
	 * Set the Center Content Pane.
	 * */
	public void setCenterPane(Container centerPane){
		mainPanel.add(centerPane, BorderLayout.CENTER);
	}
	
	
	public void config(){
	  //title
	  setTitle(LanguageLoader.getString("cap_app_name"));
	  
	  //size	
	  setSize(800, 600);
	  
	  //location
	  Dimension screSize = Toolkit.getDefaultToolkit().getScreenSize();
	  int x = screSize.width;
	  int y = screSize.height;
	  int appW = (int)getSize().getWidth();
	  int appH = (int)getSize().getHeight();
	  //display center
	  setLocation((x-appW)/2, (y-appH)/2);
	}
	
	public static StandardFrame getInstance(){
		if(standardFrame == null){
			standardFrame = new StandardFrame();
		}
		return standardFrame;
	}
	
	public AppMenuBar getAppMenuBar() {
		if(appMenuBar == null){
			appMenuBar = new AppMenuBar();
		}
		return appMenuBar;
	}
	
	public AppToolBar getAppToolBar() {
		if(appToolBar == null){
			appToolBar = new AppToolBar();
		}
		return appToolBar;
	}
	

	/**
	 * Gets the status bar.
	 * 
	 * @return the status bar
	 */
	public JXStatusBar getStatusBar() {
		if (statusBar == null) {
			statusBar = new JXStatusBar();
			JXStatusBar.Constraint c = new JXStatusBar.Constraint(JXStatusBar.Constraint.ResizeBehavior.FILL);
			statusBar.add(getLeftStatusBar(), c);
			c = new JXStatusBar.Constraint(JXStatusBar.Constraint.ResizeBehavior.FILL);
			statusBar.add(getCenterStatusBar(), c);
			c = new JXStatusBar.Constraint(JXStatusBar.Constraint.ResizeBehavior.FILL);
			statusBar.add(getRightStatusBar(), c);
		}
		return statusBar;
	}
	
	/**
	 * Gets the left status bar.
	 * 
	 * @return the left status bar
	 */
	private JLabel getLeftStatusBar() {
		if (leftStatusBar == null) {
			leftStatusBar = new JLabel(" ");
		}
		return leftStatusBar;
	}
	
	/**
	 * Sets the left status bar.
	 * @param String
	 * @param String
	 * 
	 */
	public void setLeftStatusBar(String text, String toolTip) {
		getLeftStatusBar().setText(text);
		getLeftStatusBar().setToolTipText(toolTip);
	}
	
	/**
	 * Gets the right status bar.
	 * 
	 * @return the right status bar
	 */
	private JLabel getRightStatusBar() {
		if (rightStatusBar == null) {
			rightStatusBar = new JLabel(" ");
		}
		return rightStatusBar;
	}
	
	/**
	 * Sets the right status bar.
	 * @param String
	 * @param String
	 * 
	 */
	public void setRightStatusBar(String text, String toolTip) {
		getRightStatusBar().setText(text);
		getRightStatusBar().setToolTipText(toolTip);
	}
	
	/**
	 * Gets the center status bar.
	 * 
	 * @return the center status bar
	 */
	private JLabel getCenterStatusBar() {
		if (centerStatusBar == null) {
			centerStatusBar = new JLabel(" ");
		}
		return centerStatusBar;
	}
	
	/**
	 * Sets the center status bar.
	 * @param String
	 * @param String
	 * 
	 */
	public void setCenterStatusBar(String text, String toolTip) {
		getCenterStatusBar().setText(text);
		getCenterStatusBar().setToolTipText(toolTip);
	}
}
